// haojk/pages/commissionWithdraw/index.js
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        credit2: 0.00,
        inputWithdraw: 0,
        zhifubaoNum: "",
        showView: false,
        radioValue: 1,
        cashtype: 0

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var $that = this;
    },
    changeInput: function (e) {
        this.setData({
            inputWithdraw: e.detail.value
        })

    },
    formSubmit: function (e) {
        var $that = this;

        var inputWithdraw = parseFloat($that.data.inputWithdraw);
        
        if ($that.data.inputWithdraw == null || $that.data.inputWithdraw == "" || $that.data.inputWithdraw == 0) {
            wx.showModal({
                title: '提示',
                content: '输入值不能为空或零',
                success: function (res) {
                    if (res.confirm) {
                    } else if (res.cancel) {
                    }
                }
            });
            return;
        }  
        var self=this;
            app.util.request({
                url: 'entry/wxapp/Recharge',
                data: {
                    "m": 'nets_haojk',
                    "money": inputWithdraw
                },
                showLoading: true,
                method: 'post',
                success(res) {
                    console.log("请求返回");
                    console.log(res);
                    self.setData({ "orderno": res.data.data.tid });
                    if (res.data && res.data.data) {
                        self.setData({ "prepay_id": res.data.data.package });
                        wx.requestPayment({
                            'timeStamp': res.data.data.timeStamp,
                            'nonceStr': res.data.data.nonceStr,
                            'package': res.data.data.package,
                            'signType': 'MD5',
                            'paySign': res.data.data.paySign,
                            'success': function (res) {

                                self.payresult();
                                //支付成功后，系统将会调用payResult() 方法，此处不做支付成功验证，只负责提示用户
                            },
                            'fail': function (res) {
                                console.log(res);
                                //支付失败后，
                            }
                        });
                    }
                }
            })
        
    },
    payresult: function (userInfo) {
        var $this = this;
        console.log("订单号:" + $this.data.orderno);
        app.util.request({
            url: 'entry/wxapp/RechargeResult',
            data: {
                "m": 'nets_haojk',
                "orderno": $this.data.orderno,
                "prepay_id": $this.data.prepay_id,
                "from_id": $this.data.from_id,
                "money": $this.data.applyleader_fee
            },
            method: 'post',
            success: function (response) {
                //首页存储会员对象到缓存，其他页面直接读取缓存即可
                console.log("2:");
                
                wx.setStorage({
                    key: "memberinfo",
                    data: response.data.data
                });
                $this.setData({
                    memberid: response.data.data.memberid,
                    datalock: false
                });
                wx.showModal({
                    title: '提示',
                    content: '支付成功即将返回',
                    success: function (res) {
                        if (res.confirm) {
                            wx.reLaunch({
                                url: "../partnerIndex/index"
                            });
                        } else if (res.cancel) {
                            wx.reLaunch({
                                url: "../partnerIndex/index"
                            });
                        }
                    }
                });

                //取会员对象的方式
                wx.getStorage({
                    key: 'memberinfo',
                    success: function (res) {
                    }
                })
            }
        });
    },
    
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})